<template>
  <div class="right-container">
    <el-affix :offset="80" position="top">
      <div class="notice">
        <el-icon style="margin-right: 10px"><BellFilled /></el-icon>
        <strong>通知</strong>
      </div>
      <el-carousel
        indicator-position="outside"
        trigger="click"
        height="117px"
        style="
          width: 315px;
          background-color: white;
          border-radius: 0 0 10px 10px;
          margin-bottom: 20px;
        "
      >
        <el-carousel-item v-for="(item, index) in notice" :key="index">
          <div style="text-align: center; padding: 30px">
            <h3 style="margin-bottom: 20px">{{ item.title }}</h3>
            <p>{{ item.detail }}</p>
          </div>
        </el-carousel-item>
      </el-carousel>
      <div class="guess-head">
        <span class="guess-head-left"
          ><el-icon style="margin-right: 10px"><StarFilled /></el-icon>
          <strong>猜你喜欢</strong></span
        >
        <span class="guess-head-right" @click="deReFresh">
          <span>换一批</span>
          <img
            :class="['img', refreshIndex === 0 ? 'active' : '']"
            src="@/assets/img/ic_Refresh.png"
            alt="换一批"
          />
        </span>
      </div>
      <div class="guess-content">
        <template v-if="likeListLen > 0">
          <div
            class="guess-item"
            v-for="item in likeList"
            :key="item.taskId"
            @click="handleTaskDetail(item)"
          >
            <p>
              {{ item.taskName }}
            </p>
          </div>
        </template>
        <div
          v-else-if="likeListLen === 0 || useUserStore().userMessage.userId === ''"
          style="
            display: flex;
            align-items: center;
            justify-content: center;
            height: 100%;
            color: #303133;
            cursor: pointer;
          "
          @click="handleUserInfo"
        >
          <p>找不到推荐任务，请先设置<span style="color: #2d59a2">擅长领域</span>吧</p>
        </div>
        <div v-else style="height: 100%; position: relative">
          <loader v-if="loading" color="#fff" zoom="0.2" />
        </div>
      </div>
    </el-affix>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import _ from 'lodash'
import { recommendTask } from '@/service/taskdetail/taskdetail'
import router from '@/router'
import useUserStore from '@/store/user/user'
import type { tempType } from '../../../../types/task'
import { BellFilled, StarFilled } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import Loader from '@/components/loader/loader.vue'

const notice = ref([
  { title: '通知一', detail: '系统将进行维护，请做好准备😊' },
  { title: '通知二', detail: '请您查看最新的任务分配计划😑' },
  { title: '通知三', detail: '请优先完成每一周分配的任务😔' },
  { title: '通知四', detail: '关于即将到来周二会议的提醒😣' },
  { title: '通知五', detail: '会议记录完成，正在进行总结😵' }
])
const allLikes = ref([])
const likeList = ref<tempType[]>([])
const refreshIndex = ref(-1)
const currentBatch = ref(0)
const loading = ref(false)
const likeListLen = ref(-1)

const refresh = () => {
  refreshIndex.value = 0
  // 计算下一批次的索引
  currentBatch.value = (currentBatch.value + 1) % allLikes.value.length
  likeList.value = allLikes.value[currentBatch.value]
  setTimeout(() => {
    refreshIndex.value = -1
  }, 1000)
}
const deReFresh = _.debounce(refresh, 1000, {
  leading: true,
  maxWait: 1000
})
// 设置在新页面打开任务单
const handleTaskDetail = (item: any) => {
  const href = router.resolve({
    path: '/taskdetail/' + item.taskId
  })
  window.open(href.href)
}
// 设置在当前页面打开用户主页
const handleUserInfo = () => {
  if (!useUserStore().userMessage.userId) {
    router.push('/login')
  } else {
    router.push('/main/users/' + useUserStore().userMessage.userId)
  }
}
onMounted(() => {
  if (useUserStore().userMessage.userId !== '') {
    loading.value = true
    recommendTask(20).then((res: any) => {
      console.log(res)
      if (res.code === 1) {
        // 以每组5个元素的方式对任务列表进行分组
        allLikes.value = _.chunk(res.data, 5)
        likeList.value = allLikes.value[0] // 初始化likeList为第一组数据
        likeListLen.value = likeList.value.length
        loading.value = false
      } else {
        ElMessage.error('网络异常，请刷新重试')
      }
    })
  }
})
</script>

<style lang="less" scoped>
.right-container {
  width: 250px;
  height: 300px;
  margin-left: 20px;
  .notice {
    border-bottom: 1px solid #eff2f7;
    font-size: 16px;
    color: #273444;
    line-height: 55px;
    width: 300px;
    background-color: #fff;
    padding-left: 15px;
    border-radius: 10px 10px 0 0;
  }
  .guess-head {
    font-size: 16px;
    color: #273444;
    line-height: 55px;
    width: 300px;
    background-color: #fff;
    padding-left: 15px;
    justify-content: space-between;
    display: flex;
    border-radius: 10px 10px 0 0;
    border-bottom: 1px solid #eff2f7;
    .guess-head-left,
    .guess-head-right {
      flex: 1;
      display: flex;
      align-items: center;
    }
    .guess-head-right {
      justify-content: flex-end;
      cursor: pointer;
      img {
        margin: 0 10px;
        width: 20px;
        height: 20px;
        &.active {
          animation: rotate-animation 1s infinite;
        }
      }
    }
  }
  .guess-content {
    width: 315px;
    height: 300px;
    background-color: #fff;
    border-radius: 0 0 10px 10px;
    .guess-item {
      width: 100%;
      min-height: 50px;
      border: 1px solid #eff2f7;
      transition: background-color 0.3s;
    }
    .guess-item p {
      padding: 20px;
      color: #303133;
      cursor: pointer;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .guess-item:hover {
      background-color: #f5f7fa;
    }
  }
}
@keyframes rotate-animation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
